# -*- coding: utf-8 -*-
# @Time : 2022-06-28 17:41
# @Author : Z
# @Function : 识别简单图形验证码
import time

import pytesseract
from PIL import Image
from pytesseract import image_to_string

import ddddocr

from Common.Logger import path

ocr = ddddocr.DdddOcr()
with open(path + r"\lib\verify\verify.png", 'rb') as f:
    aa = f.read()
    f.close()
    # print(aa)
res = ocr.classification(aa)
print(res)

# # 创建img对象，传入图片文件（所在路径）
# pic = Image.open(path + r"\lib\verify\verify.png")
# # print(pic)
# # 显示图片
# # pic.show()
# # 调用识别引擎，得到string类型的结果
# text = pytesseract.image_to_string(image=pic, lang='eng')
# print(text)
# a = text[:5]
# print(a)
# b = a.split(' ')
# print(b)
# dd = ''
# for key in b:
#     dd += key
# print(dd)


# # print(type(text))
# # print('')
#
#
# def depoint(image):  # 像素 判断一个点周围情况 4，8邻域
#
#     pixdata = image.load()
#     print(pixdata)
#     w, h = image.size
#     for y in range(1, h - 1):
#         for x in range(1, w - 1):
#             count = 0
#             if pixdata[x, y - 1] > 245:
#                 count += 1
#             if pixdata[x, y + 1] > 245:
#                 count += 1
#             if pixdata[x - 1, y] > 245:
#                 count += 1
#             if pixdata[x + 1, y] > 245:
#                 count += 1
#             if count > 3:
#                 pixdata[x, y] = 255
#     return image
#
#
# def binaring(image, threshold=160):
#     """
#     对传入的图像进行灰度，二值化处理
#     :param image:
#     :param threshold:
#     :return:
#     """
#     image = image.convert('L')
#     # image.show()
#     pixdata = image.load()
#     # print(pixdata)
#     w, h = image.size
#     for y in range(h):
#         for x in range(w):
#             # print(pixdata[x,y])
#             if pixdata[x, y] < threshold:
#                 pixdata[x, y] = 0
#             else:
#                 pixdata[x, y] = 255
#     return image

# if __name__ == '__main__':
#     image = Image.open(r"F:\python\Test_009\lib\verify\verify.jpg")
#     # # pix_l = []
#     # # image.show()
#     # # pix_l_set = sorted(list(set(pix_l)))
#     # # print(pix_l_set[:len(pix_l_set)//2])  # 求平均数的值
#     image2 = binaring(image)  # 二值化
#     image3 = depoint(image2)  # 降噪
#     # # image3.show()
#     # # # 识别文字
#     ll = image_to_string(image3)
#     print(ll)
